﻿using System;
using System.Collections.Generic;
using System.Linq;
using Eassets.Layered.Domain.Asset;
using Eassets.Service.Global;
using Eassets.Layered.Domain.Global;
using Eassets.Service.Asset;

namespace Eassets.UI.Asset
{
    public partial class ARNAuthorizationForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetOrganizationLevelList();
                textBoxDate.Text = DateTime.Today.ToShortDateString();

                List<Transaction> transactions = new List<Transaction>();
                gridViewTransactionsToBeAuthorized.DataSource = transactions;
                gridViewTransactionsToBeAuthorized.DataBind();
            }
        }

        private void GetOrganizationLevelList()
        {
            OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();
            List<OrganizationLevelList> organizationLevelLists = new List<OrganizationLevelList>();
            organizationLevelLists =
                organizationLevelListService.GetAllOrganizationLevelList().Where(x => x.OrganizationLevelMode == "B").
                    ToList();
            dropDownListOrganizationLevelList.DataSource = organizationLevelLists;
            dropDownListOrganizationLevelList.DataValueField = "OrganizationLevelListId";
            dropDownListOrganizationLevelList.DataTextField = "Code";
            dropDownListOrganizationLevelList.DataBind();
        }

        protected void dropDownListOrganizationLevelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropDownListOrganizationLevelList.SelectedItem.Value == "0")
            {
                List<Transaction> transactions = new List<Transaction>();

                gridViewTransactionsToBeAuthorized.DataSource = transactions;
                gridViewTransactionsToBeAuthorized.DataBind();
            }
            else
            {
                DataBindToGridViewTransactionsToBeAuthorized();
            }
        }

        private void DataBindToGridViewTransactionsToBeAuthorized()
        {
            TransactionService transactionService = new TransactionService();
            List<Transaction> transactionList = new List<Transaction>();
            transactionList =
                transactionService.GetAllTransaction().Where(
                    x =>
                    x.OrganizationLevelListId == Convert.ToInt64(dropDownListOrganizationLevelList.SelectedItem.Value) &&
                    x.TransactionStatus == "C").ToList();

            gridViewTransactionsToBeAuthorized.DataSource = transactionList;
            gridViewTransactionsToBeAuthorized.DataBind();
        }

        private void DataBindToGridViewItemDetails(string transactionNo)
        {
            TransactionService transactionService = new TransactionService();
            long transactionId = transactionService.GetTransactionIdByTransactionNo(transactionNo);

            AssetRequisitionDetailService assetRequisitionDetailService = new AssetRequisitionDetailService();
            List<AssetRequisitionDetail> assetRequisitionDetailList = new List<AssetRequisitionDetail>();
            assetRequisitionDetailList =
                assetRequisitionDetailService.GetAllAssetRequisitionDetail().Where(x => x.TransactionId == transactionId)
                    .ToList();

            gridViewItemDetails.DataSource = assetRequisitionDetailList;
            gridViewItemDetails.DataBind();
        }

        protected void gridViewTransactionsToBeAuthorized_SelectedIndexChanged(object sender, EventArgs e)
        {
            string transactionNo = gridViewTransactionsToBeAuthorized.SelectedRow.Cells[1].Text;
            DataBindToGridViewItemDetails(transactionNo);
        }
    }
}